package com.pactera.asmp.server.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.config
 * @ClassName: SpringQuartzJobFactory
 * @Description: SpringQuartzJobFactory
 * @Date 2020/08/05 17:56
 * @Author lvzhiqiang
 * @Version 1.0
 */
@Component("springQuartzJobFactory")
public class SpringQuartzJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {

    @Autowired
    private AutowireCapableBeanFactory beanFactory;

    @Override
    public void setApplicationContext(final ApplicationContext context) throws BeansException {
        beanFactory = context.getAutowireCapableBeanFactory();
    }

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object obj = super.createJobInstance(bundle);
        beanFactory.autowireBean(obj);
        return obj;
    }
}
